computeColours <- function(mapData, pal,...){
  UseMethod("computeColours")
}

computeColours.factor <- function(mapData,pal,...){
  ##
  ## make a nice palette for a factor
  ##

  if(length(pal)==1){
    if(pal=="auto"){
      ## default brewer palette
      pal = "Set3"
    }
    coloursNeeded = length(levels(mapData))
    validPalettes = rownames(brewer.pal.info)
    if(!pal %in% validPalettes){
      warning(paste("Invalid palette ",pal," using Set3 instead"))
      pal="Set3"
    }
    paletteInfo = brewer.pal.info[pal,]
    if(paletteInfo$category != "qual"){
      warning(paste("non-qualitative palette ",pal," specified for categorical data"))
    }
    if(coloursNeeded > paletteInfo$maxcolors){
      warning("Too many levels for palette - some colours will be duplicated")
    }
    nPalColours = min(coloursNeeded, paletteInfo$maxcolors)
    colours = brewer.pal(nPalColours,pal)
    if(nPalColours < coloursNeeded){
      colours=rep(colours,1 + coloursNeeded %/% nPalColours)[1:coloursNeeded]
    }
  }else{
    colours = pal
  }
  return(colours[as.numeric(mapData)])
  
}

computeColours.default <- function(mapData,pal,...){
  
  if(identical(pal,"auto")){
    
  }
  
}
